package java_concurrency_program_design_in_practice.atomic_field_updater;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * 让普通变量也享受原子操作<br/>
 * @author lixiangke
 * @date 2017/12/8
 */
public class AtomicIntegerFieldUpdaterDemo {
    public static class Candidate {
        int id;
        volatile int score;

        public final static AtomicIntegerFieldUpdater<Candidate> scoreUpdater
                = AtomicIntegerFieldUpdater.newUpdater(Candidate.class, "score");
        public static AtomicInteger allScore = new AtomicInteger();

        public static void main(String[] args) throws InterruptedException {
            final Candidate stu = new Candidate();
            Thread[] threads = new Thread[10000];
            for (int i = 0; i < 10000; i++) {
                threads[i] = new Thread() {
                    @Override
                    public void run() {
                        if (Math.random() > 0.4) {
                            scoreUpdater.incrementAndGet(stu);
                            allScore.incrementAndGet();
                        }
                    }
                };
                threads[i].start();
            }

            for (int i = 0; i < 10000; i++) {
                threads[i].join();
            }

            System.out.println("score=" + stu.score);
            System.out.println("allScore=" + allScore);
        }
    }
}
